package com.imooc.controller;

import com.imooc.api.mq.DelayConfig_Industry;
import com.imooc.base.BaseInfoProperties;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.pojo.DataDictionary;
import com.imooc.pojo.bo.DataDictionaryBO;
import com.imooc.service.DataDictionaryService;
import com.imooc.utils.GsonUtils;
import com.imooc.utils.LocalDateUtils;
import com.imooc.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @version 1.0
 * @Author Mr.Yang
 * @Date 2024-08-30 10:06
 * @question: 字典控制层
 */

@RestController
@RequestMapping("dataDict")
public class DataDictController extends BaseInfoProperties {

    @Autowired
    private DataDictionaryService dictionaryService;

    private static final String DDKEY_PREFIX = DATA_DICTIONARY_LIST_TYPECODE + ":";

    /**
     * 根据字典码获得该分类下的所有数据字典项的列表
     * @param typeCode
     * @return
     */
    @PostMapping("app/getDataByCode")
    public GraceJSONResult getDataByCode(String typeCode) {

        if (StringUtils.isBlank(typeCode)) {
            return GraceJSONResult.error();
        }

        String ddkey = DDKEY_PREFIX + typeCode;

        String ddListStr = redis.get(ddkey);
        List<DataDictionary> list = null;
        if (StringUtils.isNotBlank(ddListStr)) {
            list = GsonUtils.stringToListAnother(ddListStr, DataDictionary.class);
        }

        //只从redis中查询,如果没有就没有,也不需要从数据库中查询,完全避免缓存的穿透击穿雪崩问题
//        List<DataDictionary> list = dictionaryService.getDataByCode(typeCode);
        return GraceJSONResult.ok(list);
    }

    /**
     * 创建数据字典
     * @param dataDictionaryBO
     * @return
     */
    @PostMapping("create")
    public GraceJSONResult create(@RequestBody @Valid DataDictionaryBO dataDictionaryBO) {

        dictionaryService.createOrUpdateDataDictionary(dataDictionaryBO);
        return GraceJSONResult.ok();
    }

    /**
     * 字典列表分页
     * @param typeName
     * @param itemValue
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("list")
    public GraceJSONResult list(String typeName, String itemValue, Integer page, Integer limit) {

        if (page == null) page = 1;
        if (limit == null) limit = 10;

        PagedGridResult listResult = dictionaryService.getDataDictListPaged(typeName, itemValue, page, limit);
        return GraceJSONResult.ok(listResult);
    }

    /**
     * 修改数据字典
     * @param dataDictionaryBO
     * @return
     */
    @PostMapping("modify")
    public GraceJSONResult modify(@RequestBody @Valid DataDictionaryBO dataDictionaryBO) {

        if (StringUtils.isBlank(dataDictionaryBO.getId())) {
            return GraceJSONResult.error();
        }

        dictionaryService.createOrUpdateDataDictionary(dataDictionaryBO);
        return GraceJSONResult.ok();
    }

    /**
     * 根据id查询数据字典某一项
     * @param dictId
     * @return
     */
    @PostMapping("item")
    public GraceJSONResult item(String dictId) {
        DataDictionary dd = dictionaryService.getDataDictionary(dictId);
        return GraceJSONResult.ok(dd);
    }

    /**
     * 删除数据字典
     * @param dictId
     * @return
     */
    @PostMapping("delete")
    public GraceJSONResult delete(String dictId) {
        dictionaryService.deleteDataDictionary(dictId);
        return GraceJSONResult.ok();
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 字典数据刷新进缓存
     *  使用延迟队列实现 点击按钮后 每天凌晨三点执行
     * @return
     */
    @PostMapping("refreshDataDict")
    public GraceJSONResult refreshDataDict() {

        //计算现在到凌晨三点的时间
        LocalDateTime futureTime = LocalDateUtils.parseLocalDateTime(LocalDateUtils.getTomorrow() + " 03:00:00", LocalDateUtils.DATETIME_PATTERN);
        Long publishTimes = LocalDateUtils.getChronoUnitBetween(LocalDateTime.now(), futureTime, ChronoUnit.MILLIS, true);
//        int delayTimes = publishTimes.intValue();

        //使用10S来做测试
        int delayTimes = 10 * 1000;

        //发送延迟队列
        MessagePostProcessor processor = DelayConfig_Industry.setDelayTimes(delayTimes);
        //发送延迟消息
        rabbitTemplate.convertAndSend(
                DelayConfig_Industry.EXCHANGE_DELAY_REFRESH,
                DelayConfig_Industry.DELAY_REFRESH_DATADICT,
                "123456",
                processor);

        return GraceJSONResult.ok();
    }


}

